import { DefaultStage } from '../default-stage';
import { City } from './city';
import { Town } from './town';
import { Fortress } from './fortress';
import { Village } from './village';
import { RawAssetsUrl } from '../../raw-asset-info';
import { CommunityData } from './community-data';
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
import { PathManager } from './path-manager';
import { AbstractCommunity } from './abstract-community';
import { SelectManager } from '../select/select-manager';


class CommunityManager {

    public static readonly city: City[] = [];
    public static readonly town: Town[] = [];
    public static readonly fortress: Fortress[] = [];
    public static readonly village: Village[] = [];
    public static readonly all: AbstractCommunity[] = [];

    private static _stage: DefaultStage;

    public static initialize(stage: DefaultStage): void {
        this._stage = stage;
        CommunityData.setup(stage);
        PathManager.initialize(stage);
        this.initializeCity();
        this.initializeTown();
        this.initializeFortress();
        this.initializeVillage();
        this.all.push(...this.city, ...this.town, ...this.fortress, ...this.village);
        SelectManager.addCommunityTarget(this.all);
    }

    public static updateSovereign(): void {
        this.city.forEach(c => c.setSovereign(c.data.sovereign));
        this.town.forEach(c => c.setSovereign(c.data.sovereign));
        this.fortress.forEach(c => c.setSovereign(c.data.sovereign));
        this.village.forEach(c => c.setSovereign(c.data.sovereign));
    }

    private static initializeCity(): void {
        CommunityData.cityData.forEach((data, index) => {
            const city = new City(this._stage, {
                name: data.name || '',
                position: data.location,
                modelUrl: RawAssetsUrl.modelCheng,
                sovereign: data.sovereign,
                flagSize: 0.2,
                population: data.population,
            });
            city.dataIndex = index;
            this.city.push(city);
        });
    }

    private static initializeTown(): void {
        CommunityData.townData.forEach((data, index) => {
            const town = new Town(this._stage, {
                name: data.name || '',
                position: data.location,
                modelUrl: RawAssetsUrl.modelZhen,
                sovereign: data.sovereign,
                flagSize: 0.15,
                flagOffset: new Vector3(0, -0.15, 0),
                population: data.population,
            });
            town.dataIndex = index;
            this.town.push(town);
        });
    }

    private static initializeFortress(): void {
        CommunityData.fortressData.forEach((data, index) => {
            const fortress = new Fortress(this._stage, {
                name: data.name || '',
                position: data.location,
                modelUrl: RawAssetsUrl.modelGuan,
                sovereign: data.sovereign,
                flagSize: 0.15,
                flagOffset: new Vector3(0, -0.15, 0),
                population: data.population,
            });
            fortress.dataIndex = index;
            this.fortress.push(fortress);
        });
    }

    private static initializeVillage(): void {
        CommunityData.villageData.forEach((data, index) => {
            const village = new Village(this._stage, {
                name: data.name || '',
                position: data.location,
                modelUrl: RawAssetsUrl.modelCun,
                sovereign: data.sovereign,
                flagSize: 0.1,
                flagOffset: new Vector3(0, -0.3, 0),
                population: data.population,
            });
            village.dataIndex = index;
            this.village.push(village);
        });
    }

}


export { CommunityManager };